<template>
    <el-container class="home-container">
        <!-- 头部区域 -->
        <el-header>
            <div>
                <img src="../assets/heima_logo.png" alt="">
                <h2>电商后台管理系统</h2>
                <el-button type="danger" @click="outLogin">退出登录</el-button>
            </div>
        </el-header>
        <el-container>
            <el-aside :width="iscollapse? '65px' : '200px'">
                <div class="togbar" @click="iscollapse=!iscollapse">|||</div>
                <el-menu
                    default-active="2"
                    class="el-menu-vertical-demo"
                    background-color="#333744"
                    text-color="#fff"
                    :router="true"
                    unique-opened
                    :collapse="iscollapse"
                    :collapse-transition='false'
                    active-text-color="#ffd04b">
                    <!-- 一级菜单 -->
                    <el-submenu :index="item.id+''" v-for="(item, i) in menus" :key="item.id"
                    :class="iscollapse ? 'issmall':'isbig'">
                        <template slot="title">
                        <i :class="['iconfont',iconArr[i]]"></i>
                        <span>{{item.authName}}</span>
                        </template>
                        <!-- 二级菜单 -->
                            <el-menu-item :index="'/'+subitem.path" v-for="subitem in item.children" :key="subitem.id">
                            <i class="el-icon-menu"></i>
                            {{subitem.authName}}
                            </el-menu-item>
                    </el-submenu>
              </el-menu>
            </el-aside>
            <el-main>
              <router-view></router-view>
            </el-main>
        </el-container>
</el-container>
</template>
<script>
export default {
  data() {
    return {
      // 左侧菜单
      menus: [],
      // 图标数组
      iconArr: ['icon-user', 'icon-tijikongjian', 'icon-shangpin', 'icon-danju', 'icon-baobiao'],
      iscollapse: false
    }
  },
  created() {
    this.getmenus()
  },
  methods: {
    // 退出登录功能
    outLogin() {
      // 清除token
      window.sessionStorage.removeItem('token')
      // 强制跳转到登录页
      this.$router.push('/login')
    },
    // 获取左侧菜单
    async getmenus() {
      const { data: res } = await this.$http.get('menus')
      if (res.meta.status !== 200) return this.$message.error('获取菜单失败')
      this.menus = res.data
    }
  }
}
</script>
<style lang="less" scoped>
.home-container {
  height: 100%;
}
.el-header {
  background-color: #373d41;
  position: relative;
  img {
    position: absolute;
    top: 0;
    left: 20px;
  }
  h2 {
    margin: 0;
    padding: 0;
    padding-left: 80px;
    line-height: 55px;
  }
  .el-button {
    position: absolute;
    right: 20px;
    top: 11px;
  }
}
.el-aside {
  background-color: #333744;
  user-select: none;
}
.el-main {
  background-color: #eaedf1;
}
.togbar {
  color: #fff;
  text-align: center;
  letter-spacing: 0.2em;
  cursor: pointer;
  user-select: none;
}
.issmall {
  width: 65px;
}
.isbig {
  width: 200px;
}
</style>
